package com.wsx.ones.boots.akka;

import akka.actor.Actor;
import akka.actor.IndirectActorProducer;
import org.springframework.context.ApplicationContext;

/**
 * Created by wangshuaixin on 18/6/15.
 */
public class SpringActorProducer implements IndirectActorProducer {


    private final ApplicationContext applicationContext;
    private final String actorName;
    private final Object[] args;


    public SpringActorProducer(ApplicationContext applicationContext, String actorName) {
        this(applicationContext, actorName, null);
    }

    public SpringActorProducer(ApplicationContext applicationContext, String actorName, Object[] args) {
        this.applicationContext = applicationContext;
        this.actorName = actorName;
        this.args = args;
    }


    @Override
    public Actor produce() {
        return args == null ?
                (Actor) applicationContext.getBean(actorName) :
                (Actor) applicationContext.getBean(actorName, args);
    }

    @Override
    public Class<? extends Actor> actorClass() {
        return (Class<? extends Actor>) applicationContext.getType(actorName);
    }
}
